﻿using DocumentFormat.OpenXml.Lite.Core.Interfaces;
using DocumentFormat.OpenXml.Lite.DrawingML.Base;
using DocumentFormat.OpenXml.Lite.DrawingML.Color;
using DocumentFormat.OpenXml.Lite.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using IO = System.IO;

namespace DocumentFormat.OpenXml.Lite.DrawingML.Style
{
    [XmlRoot("theme", Namespace = Constants.Main)]
    public class Theme : XmlSerializable, ISerializable
    {
        [Optional]
        [XmlAttribute("name", Form = XmlSchemaForm.Qualified)]
        public string Name { get; set; }

        [XmlElement("themeElements", Namespace = Constants.Main)]
        public ThemeElements Elements { get; set; }

        [Optional]
        [XmlElement("objectDefaults", Namespace = Constants.Main)]
        public ObjectDefaults Defauls { get; set; }

        [Optional]
        [XmlArray("extraClrSchemeLst", Namespace = Constants.Main)]
        [XmlArrayItem("extraClrScheme", Namespace = Constants.Main)]
        public XmlList<ExtraColorScheme> ExtraColorSchemes { get; set; }

        [Optional]
        [XmlArray("custClrLst", Namespace = Constants.Main)]
        [XmlArrayItem("custClr", Namespace = Constants.Main)]
        public XmlList<CustomColor> CustomColors { get; set; }

        [Optional]
        [XmlElement("extLst", Namespace = Constants.Main)]
        public ExtensionList ExtensionList { get; set; }

        [XmlNamespaceDeclarations]
        public XmlSerializerNamespaces Namespaces { get; set; }

        public Theme()
        {
            Namespaces = new XmlSerializerNamespaces();
            Namespaces.Add("a", Constants.Main);
            Namespaces.Add("r", Constants.Relationship);

            CustomColors = new XmlList<CustomColor>();
            ExtraColorSchemes = new XmlList<ExtraColorScheme>();
        }

        #region ISerializable members

        public byte[] Serialize()
        {
            XmlSerializer serializer = new XmlSerializer(this.GetType());
            using (var stream = new IO.MemoryStream())
            {
                using (var writer = new IO.StreamWriter(stream, Encoding.UTF8))
                {
                    using (var xwriter = XmlWriter.Create(writer))
                    {
                        xwriter.WriteStartElement("a", "theme", Constants.Main);
                        foreach (var ns in Namespaces.ToArray())
                        {
                            if (ns.Name != "")
                            {
                                xwriter.WriteAttributeString("xmlns", ns.Name, null, ns.Namespace);
                            }
                        }
                        this.WriteXml(xwriter);
                    }
                    return stream.ToArray();
                }
            }
        }

        public void Deserialize(System.IO.Stream content)
        {
            using (var reader = XmlReader.Create(content))
            {
                reader.ReadToDescendant("theme", Constants.Main);
                for (int i = 0; i < reader.AttributeCount; ++i)
                {
                    string s = reader.GetAttribute(i);
                    if (Constants.GetDefaultPrefix(s) != string.Empty)
                    {
                        Namespaces.Add(Constants.GetDefaultPrefix(s), s);
                    }
                }
                this.ReadXml(reader);
            }
        }

        #endregion
    }
}
